﻿/// <reference name="MicrosoftAjax.js" /> 
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />

function DeleteAllItems() {
    if (confirm("Are you sure you want to remove all items in this list ?")) {
        this.nid = SP.UI.Notify.addNotification("In progress...", true);
        this.context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var currentListGUID = SP.ListOperation.Selection.getSelectedList();
        if (currentListGUID != null) {
            var currentList = web.get_lists().getById(currentListGUID);
            var query = SP.CamlQuery.createAllItemsQuery();
            this.allItems = currentList.getItems(query);

            context.load(allItems);
            context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded1), Function.createDelegate(this, this.onQueryFailed));
        }
    }
}

function onQuerySucceeded1() {
    var itemsToDelete = new Array();
    var listItemEnumerator = this.allItems.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var item = listItemEnumerator.get_current();
        itemsToDelete.push(item);
    }

    for (var i = itemsToDelete.length - 1; i >= 0; i--) {
        itemsToDelete[i].recycle();
    }

    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded2), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded2() {
    SP.UI.Notify.removeNotification(nid);
    SP.UI.Notify.addNotification("Done.", false);
    setTimeout("location.reload();", 3000);
}

function onQueryFailed() {
    SP.UI.Notify.addNotification("Failed.", false);
}